The parameter x
holds a reference
to the object that main()
created.
Here is a picture that shows what the executing program looks like just as the print() method starts running. Remember that Java uses call by value to pass data into methods. With array parameters, this means that the parameter holds the value of the reference to the array.
There is only one array object, the one created by main()
.
When the print()
method is running it has a reference to
this array in its parameter x
.
Here are some details:
ArrayDemo
is shown in dotted lines because it is a class definition,
not an object.ArrayDemo
's static method main()
is running.ar1
refers to the array object.operate
refers to an ArrayOps
object.print()
method of operate
is called with ar1
as a parameter.x
refers to the
same object as does the variable ar1
.
Now the print()
method runs, using the contents of
x
to refer to the array it works with.
While it is running the method works with x
just like the code in the previous chapters.
When it finishes running, control returns to main()
.
Usually you do not think about things in such excruciating detail. You would think "call a method to print the array" and would write
operate.print( ar1 );
But sometimes you really need to know what is going on. Please look the picture over and read over the details a few times. If you rush through this material, you risk missing concepts that are necessary for understanding future topics.